/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GoogleCalendarManagerEvoLoginDialog.java
 *
 * Created on 19-gen-2012, 11.33.27
 */

package googlecalendarmanagerevo;

import it.illumedellaragione.googlecalendar.ManageConfig;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JFrame;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;

/**
 *
 * @author davide.zanetti
 */
public class GoogleCalendarManagerEvoLoginDialog extends javax.swing.JDialog {


    JFrame mainFrame;
    SingleFrameApplication app;
    String username, password;
    boolean autoconnection=false;
    final String PATH_FILE_CONFIG="config.properties";

    /** Creates new form GoogleCalendarManagerEvoLoginDialog */
    public GoogleCalendarManagerEvoLoginDialog(java.awt.Frame parent, boolean modal,String u,  String p) {
        super(parent, modal);

        setLogo();

        initComponents();
        this.setUser(u);
        this.setPassword(p);
        setFields();
        if (autoconnection) Login();
    }

    /** Creates new form GoogleCalendarManagerEvoLoginDialog */
    public GoogleCalendarManagerEvoLoginDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setFields();
        if (autoconnection) Login();
    }

    public GoogleCalendarManagerEvoLoginDialog(JFrame frame) {
         super(frame, true);
         this.mainFrame = frame;
         initComponents();
         setFields();
         if (autoconnection) Login();
    }

    public GoogleCalendarManagerEvoLoginDialog(SingleFrameApplication app, boolean modal) {
         super(app.getMainFrame(),modal);
         this.app = app;
         initComponents();
         setFields();
         if (autoconnection) Login();
    }

    private void setFields(){
        jTxtUser.setText(username);
        jTxtPassword.setText(password);
    }
    
    private void saveConfig(){

        ManageConfig.setProperty(PATH_FILE_CONFIG, "user", jTxtUser.getText().trim());
        ManageConfig.setProperty(PATH_FILE_CONFIG, "pwd", jTxtPassword.getText().trim());
        //ManageConfig.setProperty(PATH_FILE_CONFIG, "autoconnection", (jCheckBoxAutoConnection.isSelected()? "true":"false"));
    }

     private void setLogo(){
        // Set Icona Applicazione
        Image img = null;
        try{
        URL url = ClassLoader.getSystemResource("googlecalendarmanagerevo/resources/logo.png");
        Toolkit kit = Toolkit.getDefaultToolkit();
        img = kit.createImage(url);
        this.setIconImage(img);
        }
        catch(Exception e){
            e.printStackTrace();
            //vado avanti e non blocco l'app
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTxtUser = new javax.swing.JTextField();
        jTxtPassword = new javax.swing.JPasswordField();
        loginButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabelLogo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(googlecalendarmanagerevo.GoogleCalendarManagerEvoApp.class).getContext().getResourceMap(GoogleCalendarManagerEvoLoginDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTxtUser.setText(resourceMap.getString("jTxtUser.text")); // NOI18N
        jTxtUser.setName("jTxtUser"); // NOI18N

        jTxtPassword.setText(resourceMap.getString("jTxtPassword.text")); // NOI18N
        jTxtPassword.setName("jTxtPassword"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(googlecalendarmanagerevo.GoogleCalendarManagerEvoApp.class).getContext().getActionMap(GoogleCalendarManagerEvoLoginDialog.class, this);
        loginButton.setAction(actionMap.get("Login")); // NOI18N
        loginButton.setText(resourceMap.getString("loginButton.text")); // NOI18N
        loginButton.setName("loginButton"); // NOI18N

        cancelButton.setAction(actionMap.get("Cancel")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N

        jLabelLogo.setIcon(resourceMap.getIcon("jLabelLogo.icon")); // NOI18N
        jLabelLogo.setText(resourceMap.getString("jLabelLogo.text")); // NOI18N
        jLabelLogo.setName("jLabelLogo"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(146, 146, 146)
                        .addComponent(jLabelLogo))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(loginButton)
                                .addGap(18, 18, 18)
                                .addComponent(cancelButton)
                                .addGap(117, 117, 117))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTxtUser, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE)
                                    .addComponent(jTxtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 314, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelLogo)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTxtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTxtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(loginButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GoogleCalendarManagerEvoLoginDialog dialog = new GoogleCalendarManagerEvoLoginDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void Login() {
        username=this.jTxtUser.getText().trim();
        password=this.jTxtPassword.getText().trim();
        this.saveConfig();
        dispose();
    }

    @Action
    public void Cancel() {

        //dispose();
        System.exit(-1);

    }

    public String getUser(){
        return username;
    }

    public String getPassword(){
        return password;
    }


    public void setUser(String s){
        username=s;
    }

    public void setPassword(String s){
        password=s;
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabelLogo;
    private javax.swing.JPasswordField jTxtPassword;
    private javax.swing.JTextField jTxtUser;
    private javax.swing.JButton loginButton;
    // End of variables declaration//GEN-END:variables

}
